<!-- 本示例未包含完整css，获取外链css请参考上文，在hello uni-app项目中查看 -->
<template>
	<view class="bg-white">
		<titleBar title="发布提问" isReturn="true" textAlign="center"></titleBar>
		<view class="padding-top">

			<uni-forms ref="form" :value="formData" :rules="rules" class="padding-top" >
				<uni-forms-item label="邮箱地址" name="email" labelAlign="left" labelWidth="75px">
					<uni-easyinput v-model="formData.email" placeholder="请输入邮箱地址" />
				</uni-forms-item>
				<jinEdit placeholder="请描述具体内容" ref="editComponents" v-model="formData.question" @editOk="editOk" :uploadFileUrl="uploadFileUrl">
				</jinEdit>
			</uni-forms>

		</view>
		<view class=" padding flex flex-direction bg-white">
			<button class="cu-btn bg-gradual-blue lg" @tap="submit">提交</button>
		</view>



	</view>
</template>
<script>
	import titleBar from '@/components/titleBar/titleBar.vue'

	import jinEdit from '@/components/jin-edit/jin-edit.vue'
	import {
		userEmail
	} from "@/utils/validate.js"
	export default {
		components: {

			titleBar,

			jinEdit

		},


		data() {
			return {

				uploadFileUrl: 'https://www.turkeylike.cn:8900/fzulab/article/upload',
				placeholder: '开始输入...',
				messageForm: {
					question: '',
					email: '',
					userid:uni.getStorageSync("userid")
				},
				formData: {
					question: '',
					email: '',
				},
				rules: {
					
					// 对question字段进行必填验证
					question: {
						rules: [{
							required: true,
							errorMessage: '请描述具体问题',
						}]
					},
					// 对email字段进行必填验证
					email: {
						rules: [{
							required: true,
							errorMessage: '请输入邮箱地址',
						}, {

							validateFunction: function(rule, value, data, callback) {

								if (!userEmail(value)) {
									// 通过返回 resolve
									callback('请输入正确邮箱地址！')

								}

								return true
							}
						}, ]
					},


				},
			}
		},
		onLoad(option) {
			console.log(uni.getStorageSync("userid"));
		},
		onReady() {

			// 需要在onReady中设置规则
			// 使用 validateFunction 自定义校验规则,则不能采用 uni-forms 的 rules 属性来配置校验规则，这时候需要通过ref，在onReady生命周期调用组件的setRules方法绑定验证规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			editOk(res) {
				/* 点击的时候触发对应的数据事件 */
				this.messageForm.question = res.html;
				


			},

			submit(e) {

				//验证表单
				this.$refs.form.submit().then(res => {


					//修改密码
					uni.showModal({
						title: '提示',
						question: '确定要留言吗？',
						cancelText: '取消',
						confirmText: '确定',
						success: res => {
							if (res.confirm) {

								this.$refs.form.submit().then(res => {
								
									console.log(this.formData.question)

									// this.messageForm.question = res.question
									this.messageForm.email = res.email

									// JSON.stringify(this.messageForm.question),
									var that = this
									console.log(JSON.stringify(this.messageForm.question))
									console.log(this.messageForm.email);
									debugger
									uni.request({


										url: "https://www.turkeylike.cn:8900/fzulab/userQuestion/addUserQuestion",
										method: 'post',
										data: {

											question: JSON.stringify(this.messageForm.question),
											userid:this.messageForm.userid
										},

										header: {
											'question-type': 'application/json;charset-utf-8',

											'token': uni.getStorageSync('token')


										},
										
										success(res) {
											debugger
											if (res.data.code == '500') {
												uni.showToast({
													title: res.data.msg,
													icon: 'none',
													duration: 2000
												});
												that.loading = false;
												return
											}

											uni.showToast({
												title: "添加留言成功！",
												icon: 'none',
												duration: 2000
											});
											

											
											that.formData.email = ''
                                           
											that.$refs.editComponents.clear();
											let currentPage =
												getCurrentPages()
												.pop(); //当前页面
											if (currentPage ==
												undefined ||
												currentPage == null
											) return;
											currentPage
												.onLoad(); //页面存在刷新当前页面




										}
									})

								}).catch(err => {

									console.log('表单错误信息：', err);
								})

							}
						}
					})
				}).catch(err => {

					console.log('表单错误信息：', err);

				})








			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 200rpx 0;
	}

	.container {
		padding: 10px;
	}

	#editor {
		width: 100%;
		height: 300px;
		background-color: #CCCCCC;
	}

	button {
		margin-top: 10px;
	}
</style>
